package com.xxrl.shop.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.xxrl.shop.common.entity.JsonResult;
import com.xxrl.shop.common.entity.ResultCode;
import com.xxrl.shop.common.utils.ResultTool;
import com.xxrl.shop.domain.Address;
import com.xxrl.shop.domain.User;
import com.xxrl.shop.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author mis
 */
@Api(tags = {"address"})
@RestController
@RequestMapping("/address")
public class AddressController {

    private final AddressService addressService;

    public AddressController(AddressService addressService) {
        this.addressService = addressService;
    }

    @ApiOperation(value = "List all address of user", response = Address.class, responseContainer = "List")
    @GetMapping("list")
    public JsonResult<String> list(@RequestParam(name = "userId") Long userId) {
        List<Address> userAddressList = addressService.findUserAddressList(userId);
        JSONArray jsonArray = new JSONArray();
        jsonArray.addAll(userAddressList);
        return ResultTool.success(jsonArray.toString());
    }

    @ApiOperation(value = "Get address by id", response = Address.class)
    @GetMapping("find")
    public JsonResult<String> find(@RequestParam(name = "addressId") Long id) {
        Address address = addressService.find(id);
        if (address != null) {
            return ResultTool.success(JSON.toJSON(address).toString());
        } else {
            return ResultTool.fail(ResultCode.COMMON_FAIL);
        }
    }

    @ApiOperation(value = "Update address content")
    @PostMapping("edit")
    public JsonResult<String> edit(@RequestParam(name = "id") Long addressId,
                                   @RequestParam(name = "receiver") String receiver,
                                   Long userId,
                                   String phone,
                                   String streetName,
                                   Boolean isDefault) {
        // 确保只有一个默认
        if (isDefault) {
            addressService.resetUserDefault(userId);
        }
        addressService.edit(addressId, receiver, phone, streetName, isDefault);
        return  ResultTool.success();
    }

    @ApiOperation(value = "Add a new address for user")
    @PostMapping("add")
    public JsonResult<String> add(@ApiParam(value = "New address.", hidden = true) Address address, Long userId) {
        address.setUser(new User(userId));
        // 确保只有一个默认
        if (address.getIsDefault()) {
            addressService.resetUserDefault(userId);
        }
        addressService.save(address);
        return ResultTool.success();
    }

    @ApiOperation(value = "Remove a exist address")
    @PostMapping("delete")
    public JsonResult<String> delete(@RequestParam(name = "id") Long addressId) {
        addressService.delete(addressId);
        return  ResultTool.success();
    }
}
